﻿using System;
using System.Collections.Generic;
using System.Text;
using EmbeddedSharp.Core;

namespace EmbeddedSharp.Core.Function.Math {

    /// <summary>
    /// Get the fractional part of the number
    /// </summary>
    [Function("frationalPart", "frationalPart(\"\")", "n=1.32;d=frationalPart(n);echo(d);", "d==0.32")]
    public class FractionalPartNode : FuncNode {

        public FractionalPartNode()
            : base() {
            this.Value = "frationalPart";
        }

        public override object Evaluate() {
            if (Parameters.Count < 1) {
                throw new ArgumentException("usage: frationalPart(number)");
            }

            decimal value = Convert.ToDecimal(Parameters[0].Evaluate());
            decimal integer = Decimal.Round(value, 0);

            return value - integer;
        }
    }
}
